package leetcode._0169;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 169. Majority Element
 * Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
 *
 * You may assume that the array is non-empty and the majority element always exist in the array.
 *
 * Example 1:
 *
 * Input: [3,2,3]
 * Output: 3
 * Example 2:
 *
 * Input: [2,2,1,1,1,2,2]
 * Output: 2
 */
public class Solution {
    /**
     * 暴力解，把每一个数的个数统计一遍，最后该数的个数是否大于n/2
     * 时间复杂度O(n^2)，空间复杂度O(1)
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        int majorityCount = nums.length / 2;
        for (int i = 0; i < nums.length; i++) {
           int count = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[i] == nums[j]) {
                    count++;
                }
            }
            if (count > majorityCount) {
                return nums[i];
            }
        }
        return -1;
    }

    /**
     * hashmap,
     * 现将各个数字的个数统计出来，后统计出来的数总找个数最多的
     * HashMap<数字:Integer, 数字的个数:Integer>
     * 时间复杂度是O(2n) = O(n), 空间复杂度O(n)
     */
    public int majorityElement1(int[] nums) {
        HashMap<Integer, Integer> countMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!countMap.containsKey(nums[i])) {
                countMap.put(nums[i], 1);
            } else {
                countMap.put(nums[i], countMap.get(nums[i])+1);
            }
        }
        Map.Entry<Integer, Integer> majorityEntry = null;
        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {
                majorityEntry = entry;
            }
        }
        return majorityEntry.getKey();
    }


    /**
     * 排序后，中间的值n/2 ,n/2+1;
     * 时间复杂度nlogn，o(1), O(n)
     * @param nums
     * @return
     */
    public int majorityElement2(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }

    /**
     * 随机一个index，再统计这个index数有多少个。
     * 按照这种来说，随机到预想要的值得几率比较大。毕竟占了1/2呀
     * @param nums
     * @return
     */
    public int majorityElement3(int[] nums){
        Random random = new Random();
        int majorityCount = nums.length / 2;
        while (true) {
            int candidate = nums[randRange(random, 0, nums.length)];
            if (countCandidate(nums, candidate) > majorityCount) {
                return candidate;
            }
        }
    }

    private int countCandidate(int[] nums, int candidate) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == candidate) {
                count ++;
            }
        }
        return count;
    }

    private int randRange(Random random, int min, int max) {
        return random.nextInt(max - min) + min;
    }




    public int majorityElement5(int[] nums) {
        int count = 0;
        Integer candidate = null;

        for (int num : nums) {
            if (count == 0) {
                candidate = num;
            }
            count += (num == candidate) ? 1 : -1;
        }

        return candidate;

    }
}
